/*All action related with post method from admin board*/
var Form_Obj;

		
/*post validation*/
var remote_url = home_url + '/validation';
var media_url = home_url + '/wp-content/uploads/';
var current_url = window.location.href;

if (current_url.indexOf('wp-admin/post-new.php')!=-1 || current_url.indexOf('wp-admin/post.php')!=-1) {
	/*post validation method build up*/
	jQuery.validator.addMethod(
		'title_one_word', 
		function(value, element) { 
			if (value.indexOf(' ') == -1) {
				return false;
			} 
			return true;
		}, 
		'Please input more than 1 word for the title'
	);
	
	jQuery.validator.addMethod(
		'title_first_space', 
		function(value, element) { 
			if (value.indexOf(' ') == 0) {
				return false;
			}
			return true;
		}, 
		'Please clear the space character at the beginning of the title'
	);
	
	jQuery.validator.addMethod(
		'internal_media', 
		function(value, element) { 
			if (value.indexOf(media_url) == -1) {
				return false;
			}
			return true;
		}, 
		'Please upload or choose a right media file from library'
	);
	
	jQuery.validator.addMethod(
		'blogspot_host', 
		function(value, element) { 
			if (value.indexOf('.blogspot.') == -1) {
				return false;
			}
			return true;
		}, 
		'Please input the right blogspot URL'
	);
	
	jQuery.validator.addMethod(
		'content_more_words', 
		function(value, element) { 
			index1 = 0;
			for (count = 0; count < 150;){
				index1 = value.indexOf(' ', index1);
				if (index1 == -1) break;
				index2 = value.indexOf(' ', index1);
				if (index2 == -1) break;
				if (index2 > index1 + 1) {
					count++;
				}
				index1 = index2;
			}
	
			return true;
		}, 
		'Please input more than 150 words'
	);
	
	
	
	
	jQuery("#publish").click(function(){
		jQuery("#post").validate({
	        errorClass: "wpcf-form-error",
	        errorPlacement: function(error, element){
	            error.insertAfter(element);
	        },
	        highlight: function(element, errorClass, validClass) {
	            jQuery(element).parents('.collapsible').slideDown();
	            jQuery("input#publish").addClass("button-primary-disabled");
	            jQuery("input#save-post").addClass("button-disabled");
	            jQuery("#save-action .ajax-loading").css("visibility", "hidden");
	            jQuery("#publishing-action #ajax-loading").css("visibility", "hidden");
			},
	        unhighlight: function(element, errorClass, validClass) {
				jQuery("input#publish, input#save-post").removeClass("button-primary-disabled").removeClass("button-disabled");
			}
	    });
		/*common validation*/
		var Rule_Obj = 'input[name="post_title"]';
		jQuery(Rule_Obj).rules("add", {
			required: true,
			minlength: 10,
			maxlength: 150,
			title_one_word: true,
			title_first_space: true
		});
		
		
		/*Post type = templates*/
		if (current_url.indexOf('post_type=templates') != -1) {
			Rule_Obj = 'input[name="wpcf[template-preview-image]"]';
			jQuery(Rule_Obj).rules("add", {
				required: true,
				minlength: (media_url.length),
				maxlength: (media_url.length + 150),
				url: true,
				accept: "jpg|png|bmp|gif",
				internal_media: true
			});
			
			Rule_Obj = 'input[name="wpcf[template-preview-url]"]';
			jQuery(Rule_Obj).rules("add", {
				required: true,
				minlength: ('http://.blogspot.ru'.length),
				maxlength: ('http://.blogspot.ru'.length + 150),
				url: true,
				blogspot_host: true
			});
			
			Rule_Obj = 'input[name="wpcf[template-download-url]"]';
			jQuery(Rule_Obj).rules("add", {
				required: true,
				minlength: (media_url.length),
				maxlength: (media_url.length + 150),
				accept: "zip",
				url: true,
				internal_media: true
			});
		}
		
		/*Post type = widgets*/
		if (current_url.indexOf('post_type=widgets') != -1) {
			Rule_Obj = 'input[name="wpcf[widget-preview-image]"]';
			jQuery(Rule_Obj).rules("add", {
				required: true,
				minlength: (media_url.length),
				maxlength: (media_url.length + 150),
				url: true,
				accept: "jpg|png|bmp|gif",
				internal_media: true
			});
			
			Rule_Obj = 'textarea[name="wpcf[widget-content]"]';
			jQuery(Rule_Obj).rules("add", {
				required: true,
				minlength: 150,
				maxlength: 2000,
				content_more_words: true
			});
		}
		
		
		
	});
		
		
	/*post_type=widgets, form builder for widget form shortcode*/
	var form_builder_shortcode_button = 
	[
		{'name':'checkbox','value' :'[checkbox title="title" name="var_name" value="number or string" guide="Guide to your checkbox"/]'},
		{'name':'text','value' :'[text min="0" max="2000" require="false" url="false" image="false" number="false" title="title" name="var_name" guide="Guide to your single text"/]'},
		{'name':'textarea','value' :'[textarea min="0" max="2000" require="false" title="title" name="var_name" guide="Guide to your textarea"/]'},
		{'name':'radio','value' :'[radio title="title" name="var_name" value="number or string" guide="Guide to your radio"/]'},
		{'name':'select','value' :'[select title="title" name="var_name" guide="Guide to your select"]\n[option]Value 1[/option]\n[option]Value 2[/option]\n[/select]'}
	];
	
	var form_shortcode_button_css = 'style="padding: 5px;margin: 0 10px 5px 2px;background: grey;display: block;float: left;color: white;cursor: pointer;"';
	
	jQuery(document).ready(function($) {
		$('textarea[name="wpcf[widget-form-shortcode]"]').attr('rows', 15);
		$('textarea[name="wpcf[widget-form-shortcode]"]').css('font-size', '16px');
		$('textarea[name="wpcf[widget-form-shortcode]"]').css('font-family', 'Courier, courier new');
		
		// generate add shortcode buttons
		html = '';
		
		for (var i = 0; i < form_builder_shortcode_button.length; i++) {
			html += '<span '+form_shortcode_button_css+' class="form_shortcode" data="'+form_builder_shortcode_button[i].name+'">'+form_builder_shortcode_button[i].name+'</span>';		
		}
		
		$(html).insertBefore('textarea[name="wpcf[widget-form-shortcode]"]');
		
		// process when a shortcode buttons clicked
		$('.form_shortcode').live('click', function(){
			data = $(this).attr('data');
			for (var i = 0; i < form_builder_shortcode_button.length; i++) {
				if (form_builder_shortcode_button[i].name == data) {
					value = $('textarea[name="wpcf[widget-form-shortcode]"]').val();
					value += form_builder_shortcode_button[i].value + '\n\n';
					$('textarea[name="wpcf[widget-form-shortcode]"]').val(value);
					break;
				}
			}
		});
	});
	
}